﻿CREATE FUNCTION Collection_GroupFilter(
	@CompanyID int,
	@DPD int
)RETURNS varchar(16) WITH SCHEMABINDING AS BEGIN

DECLARE @Group varchar(16)

SET @Group = 
CASE @CompanyID
	WHEN 1 THEN 'AFC'
	WHEN 2 THEN 'BQ'
	WHEN 4 THEN 'AFC'
	WHEN 5 THEN 'SF'
	ELSE 'XXX'
END


SET @Group = @Group +'_'+ 
CASE 
	WHEN @DPD <=               0 THEN '00'
	WHEN @DPD BETWEEN 01 AND 07 THEN '01_07'
	WHEN @DPD BETWEEN 08 AND 15 THEN '08_15'
	WHEN @DPD BETWEEN 16 AND 30 THEN '16_30'
	WHEN @DPD BETWEEN 31 AND 70 THEN '31_70'
	WHEN @DPD BETWEEN 71 AND 60 THEN '71_90'
	ELSE '91_179'
END

RETURN @Group
END

